<include file="Public/header" />


<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <!-- <div class="layui-inline">
                        <label class="layui-form-label">文章ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="aid" placeholder="请输入" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">作者</label>
                        <div class="layui-input-inline">
                            <input type="text" name="uid" placeholder="请输入" autocomplete="off" class="layui-input"></div>
                    </div> -->
                    <div class="layui-inline">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">文章标签</label>
                        <div class="layui-input-inline">
                            <select name="article_type">
                                <option value="">请选择标签</option>
                                <option value="1">外汇基础知识</option>
                                <option value="2">外汇名词解释</option>
                                <option value="3">网站公告</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button>
                    <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
                </div>

                <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
                
                <script type="text/html" id="table-content-list">
                    <a class="layui-btn layui-btn-normal layui-btn-xs edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a> 
                    <a class="layui-btn layui-btn-danger layui-btn-xs del" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a> 
                </script>
            </div>
        </div>
    </div>
<include file="Public/bottom" />
</body>

</html>
<script>



    layui.config({
        base: '__PUBLIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload','table'],function() {
        
        var table = layui.table,
        form = layui.form;

        // 时间戳 转为 时间格式
        window.createTime = function(v){
            var date = new Date(v * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            return Y+M+D+h+m+s;
        }

        // 数据表格
        table.render({
            elem: '#LAY-app-content-list'
            ,url:   "{:u('')}"
            ,page: true //开启分页
            ,cols: [[
                {field: 'id', title: 'ID',  sort: true, width:70, fixed: 'left'}
                ,{field: 'title_zh', title: '标题',width:400}
                ,{field: 'is_show', title: '是否显示',  sort: true,templet:function(res){
                    return res.is_show == 1 ? '是':'<b style="color:red;">否</b>';
                }}
                ,{field: 'username', title: '发布者'}
                ,{field: 'article_type', title: '文章类型',templet:function(res){
                    switch(res.article_type){
                        case '1' :
                            return '外汇基础知识';
                            break;
                        case '2' :
                            return '<b style="color:red;">外汇名词解释</b>';
                            break;
                        case '3' :
                            return '<b style="color:#009688;">公司公告</b>';
                            break;
                    }
                    // return res.article_type == 1 ? '外汇基础知识':'<b style="color:red;">外汇名词解释</b>';
                }}
                ,{field: 'add_time', title: '添加时间', sort: true,templet:function (row){
                    return createTime(row.add_time);
                }}
                ,{field: '', title: '操作', templet:'#table-content-list'}
            ]]
            ,id: 'testReload'
            ,title_zh: 'testReload'
            ,uid: 'testReload'
            ,aid: 'testReload'
            ,article_type: 'testReload'
            ,page:{
                limit:10
            }
            ,done:function(){
                layer.closeAll();
            }
        });


        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data) {
            var field = data.field;

            layer.msg('查询中请稍候...',{icon:16,time:false,shade:0.2});
            //执行重载
            table.reload('testReload', {
                page:{
                    curr:1
                }
                ,where:{
                    key:{
                        title_zh: field.title
                        ,uid: field.uid
                        ,aid: field.aid
                        ,article_type: field.article_type
                    }
                }
            },function(){
                layer.colseAll();
            });
        });




        // 编辑 删除 事件
        table.on('tool()' , function(obj){
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '添加文章',
                    content: "edit/id/"+data.id,
                    maxmin: true,
                    area: ['1250px', '650px'],
                    // btn: ['确定', '取消'],
                    yes: function(index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }else if(obj.event === 'del'){
                console.log(obj);

                layer.confirm('确定要删除本条数据吗？',{},function(){
                    layer.load(1);
                    $.get('del/id/'+data.id,function(res){
                        layer.closeAll();
                        if (res.status == 1) {
                            obj.del();
                            layer.msg(res.info,{icon:1},function(){});
                        }else{
                            layer.msg(res.info)
                        }
                    });
                })
            }
        });


        var $ = layui.$, active = {
            batchdel: function() {
                var checkStatus = table.checkStatus('LAY-app-content-list'),
                checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.confirm('确定删除吗？',
                function(index) {

                    //执行 Ajax 后重载
                    /*
                  admin.req({
                    url: 'xxx'
                    //,……
                  });
                  */
                    table.reload('LAY-app-content-list');
                    layer.msg('已删除');
                });
            },
            add: function() {
                layer.open({
                    type: 2,
                    title: '添加文章',
                    content: '{:U(addArticle)}',
                    maxmin: true,
                    area: ['1250px', '650px'],
                    // btn: ['确定', '取消'],
                    yes: function(index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>